package blog.sys.service;

import blog.sys.entity.Corpus;
import blog.sys.entity.FollowItem;
import blog.sys.entity.Special;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pub.dao.GeneralDao;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class FollowItemService {


    public List<Special> onChoiceSpecialCont(Integer userId, Integer typeId) {
        String sql = "SELECT * FROM t_follow_item f WHERE f.user_id = ? and f.type_id = ?";
        List<FollowItem> list = generalDao.queryList(FollowItem.class, sql, userId, typeId);
        List<Special> specials = new ArrayList<>();
        for (FollowItem item : list) {
            int id = item.getFollow_id();
            String sql2 = "SELECT s.*,(select c.title from t_release_content c where c.id = MAX(d.release_id)) as title FROM t_special s LEFT JOIN t_contributions_detail d on s.id = d.special_id and d.status = 1  WHERE s.id = ? and s.status = 0 ORDER BY d.id desc ";
            Special special = generalDao.queryValue(Special.class, sql2, id);
            specials.add(special);
        }
        return specials;
    }

    public List<Corpus> onChoiceCorpusCont(Integer userId, Integer typeId) {
        String sql = "SELECT * FROM t_follow_item f WHERE f.user_id = ? and f.type_id = ?";
        List<Corpus> list = generalDao.queryList(Corpus.class, sql, userId, typeId);
        return list;
    }

    @Autowired
    private GeneralDao generalDao;

}
